<?php
/**
 * Jishupu-Laravel-Vue-Admin
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Controllers;

use App\Http\Requests\InfoModelRequest;
use App\Http\Resources\InfoModelCollection;
use App\Models\InfoModel;
use Illuminate\Http\Request;

/**
 * 信息模型模块
 * Class InfoModelController
 * @package App\Http\Controllers
 */
class InfoModelController extends Controller
{

    /**
     * 列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $paginate = InfoModel::paginate($this->pageSize());
        $result = InfoModelCollection::make($paginate)->toArray(null);
        return $this->success('', $result);
    }

    /**
     * 添加
     * @param InfoModelRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(InfoModelRequest $request)
    {
        $data = $request->validate('store');
        $infoModel = new InfoModel($data);
        if (!$infoModel->save()) return $this->errorStored();
        return $this->successStored();
    }

    /**
     * 信息栏目分类的模型选项配置
     * @return \Illuminate\Http\JsonResponse
     */
    public function infoCategoryModelConfig() {
        $item = InfoModel::select('id', 'name')->get()->toArray();
        return $this->success('', ['items' => $item]);
    }
}
